﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class doanh_nghiep_dn_cap_nhat_km : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                using (babydealEntities context = new babydealEntities())
                {
                    var rs = from km in context.khuyen_mai
                             where km.id == id
                             select km;
                    babydealModel.khuyen_mai k = new babydealModel.khuyen_mai();
                    k = rs.FirstOrDefault();
                    if (k != null)
                    {
                        if(k.id_the_loai != null)
                            the_loai.SelectedValue = k.id_the_loai.ToString();
                        mo_ta_sp.Text = k.mo_ta_sp;
                        noi_dung.Text = k.noi_dung;
                        dieu_kien_ap_dung.Text = k.dieu_kien_ap_dung;
                        dac_trung.Text = k.dac_trung;
                        gia_goc.Text = k.gia_goc.ToString();
                        gia_km.Text = k.gia_km.ToString();
                        ngay_bat_dau.Text = k.ngay_bat_dau.ToString();
                        ngay_ket_thuc.Text = k.ngay_ket_thuc.ToString();
                        so_luong_toi_da.Text = k.so_luong_toi_da.ToString();
                        so_luong_toi_thieu.Text = k.so_luong_toi_thieu.ToString();
                        ten_sp.Text = k.ten_sp;
                        if (k.anh_1 != null)
                            anh_1.ImageUrl = "../upload/" + k.anh_1;
                        if (k.anh_2 != null)
                            anh_2.ImageUrl = "../upload/" + k.anh_2;
                        if (k.anh_3 != null)
                            anh_3.ImageUrl = "../upload/" + k.anh_3;
                        if (k.anh_4 != null)
                            anh_4.ImageUrl = "../upload/" + k.anh_4;
                        if (k.anh_5 != null)
                            anh_5.ImageUrl = "../upload/" + k.anh_5;
                    }
                }
            }
        }
    }
    protected void cap_nhat_km_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            using (babydealEntities db = new babydealEntities())
            {
                babydealModel.khuyen_mai km = (from k in db.khuyen_mai
                                               where k.id == id
                                               select k).FirstOrDefault();
                if (km == null)
                {
                    return;
                }
                km.ten_sp = ten_sp.Text;

                //Lấy id của thằng đang login
                km.id_the_loai = Convert.ToInt32(the_loai.SelectedValue);
                string str_mo_ta_sp = mo_ta_sp.Text;
                string str_noi_dung = noi_dung.Text;
                string str_dk_ap_dung = dieu_kien_ap_dung.Text;
                string str_dac_trung = dac_trung.Text;
                int iGiaGoc = Convert.ToInt32(gia_goc.Text);
                int iGiaKM = Convert.ToInt32(gia_km.Text);
                DateTime dNgayBatDau = Convert.ToDateTime(ngay_bat_dau.Text);
                DateTime dNgayKetThuc = Convert.ToDateTime(ngay_ket_thuc.Text);
                int iSoLuongToiDa = Convert.ToInt32(so_luong_toi_da.Text);
                int iSoLuongToiThieu = Convert.ToInt32(so_luong_toi_thieu.Text);

                string savePath = Server.MapPath("~/upload/");
                if (hinh_anh_1.HasFile)
                {
                    string fileName = (km.anh_1 == null) ? hinh_anh_1.FileName : km.anh_1;
                    hinh_anh_1.SaveAs(savePath + fileName);
                    km.anh_1 = fileName;
                }
                if (hinh_anh_2.HasFile)
                {
                    string fileName = (km.anh_2 == null) ? hinh_anh_2.FileName : km.anh_2;
                    hinh_anh_2.SaveAs(savePath + fileName);
                    km.anh_2 = fileName;
                }
                if (hinh_anh_3.HasFile)
                {
                    string fileName = (km.anh_3 == null) ? hinh_anh_3.FileName : km.anh_3;
                    hinh_anh_3.SaveAs(savePath + fileName);
                    km.anh_3 = fileName;
                }
                if (hinh_anh_4.HasFile)
                {
                    string fileName = (km.anh_4 == null) ? hinh_anh_4.FileName : km.anh_4;
                    hinh_anh_4.SaveAs(savePath + fileName);
                    km.anh_4 = fileName;
                }
                if (hinh_anh_5.HasFile)
                {
                    string fileName = (km.anh_5 == null) ? hinh_anh_5.FileName : km.anh_5;
                    hinh_anh_5.SaveAs(savePath + fileName);
                    km.anh_5 = fileName;
                }
                km.mo_ta_sp = str_mo_ta_sp;
                km.dac_trung = str_dac_trung;
                km.gia_goc = iGiaGoc;
                km.gia_km = iGiaKM;
                km.ngay_bat_dau = dNgayBatDau;
                km.ngay_ket_thuc = dNgayKetThuc;
                km.so_luong_toi_thieu = iSoLuongToiThieu;
                km.so_luong_toi_da = iSoLuongToiDa;
                km.noi_dung = str_noi_dung;
                km.dieu_kien_ap_dung = str_dk_ap_dung;

                db.SaveChanges();
                Response.Redirect("./khuyen_mai.aspx");
            }
        }
    }
}